package site.sorghum.ddl.compare;

import site.sorghum.ddl.entity.DdlIndexWrap;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class IndexCompare {

    /**
     * 比较返回新增的索引列
     *
     * @return 新增的索引列
     */
    public static List<DdlIndexWrap> compare(List<DdlIndexWrap> javaDdlColumnWraps, List<DdlIndexWrap> dbDdlColumnWraps) {
        // 如果数据库中没有列，则所有新列都是新增的
        if (dbDdlColumnWraps == null || dbDdlColumnWraps.isEmpty()) {
            return new ArrayList<>(javaDdlColumnWraps);
        }

        // 如果新列列表为空，则没有新增列
        if (javaDdlColumnWraps == null || javaDdlColumnWraps.isEmpty()) {
            return new ArrayList<>();
        }

        // 获取数据库列名集合
        List<String> dbColumnNames = dbDdlColumnWraps.stream().map(DdlIndexWrap::getIndexName).collect(Collectors.toList());
        return javaDdlColumnWraps.stream()
                .filter(column -> !dbColumnNames.contains(column.getIndexName()))
                .collect(Collectors.toList());
    }
}
